<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
 * Created on 2012-7-25
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 class role {
		/*
		 * 获取用户权限链表actiongrouptree-groupid_actionid(增加的权限)-groupid_actionid(减少的权限)
		 */
		private $CI;
		function __construct(){
			$this->CI = &get_instance();
		}
		function get_action_list($userid){
			$CI = $this->CI;
			$CI->load->model('user_m');
			$query = $CI->user_m->get_action_list($userid)->result_array();
			$query =$query[0];
			return  implode('-',$query);
		}
		/*
		 * 判断有没有该menu列表，如果没有则生成。
		 */
		function create_menu_files($actionlist){

			$filepath = APPPATH.'views/admin/files/menus/';
			$filename = $filepath.$actionlist.'.php';
			$menus = $this->get_menus($actionlist);
			ob_start();
			echo "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');?>\n";
			echo "\t<ul id='menu_li'>\n\t\t<li class='menu_list'>\n";
			foreach($menus as $menu){
				echo "
				<li class='menu_list'>
					<a href='".$menu['menualias']."' class='menu current'>".$menu['menuname']."</a>
					<ul class='sub_menu'>";
					foreach($menu['groups'] as $group){
						echo "
						<li>
							<a href=''>".$group['groupname']."</a>
						</li>";
					}
				echo "
					</ul>
				</li>";
			}
			echo "
			</li>
	</ul>";
			$content = ob_get_contents();
			ob_end_clean();
			    if(!$handle = fopen($filename, 'w')) {
			         echo "不能打开文件 $filename";
			         exit();
			    }
			    if(fwrite($handle, $content) === FALSE) {
			        echo "不能写入到文件 $filename";
			        exit();
			    }
		}
		function get_menus($actionlist){
			$CI = $this->CI;
			$list = explode('-',$actionlist);
			$actionadd = explode('_',$list['1']);
			$actioncut = explode('_',$list['2']);
			$CI->load->model('rolecontrol_m');
			$actioninfos = $CI->rolecontrol_m->get_actioninfos_by_roleid($list['0'],$actionadd);
			for($i=0;$i<count($actioninfos);$i++){
				for($j=0;$j<count($actioncut);$j++){
					if($actioninfos[$i]['actionid']==$actioncut[$j]){
						$actioninfos[$i]=null;
					}

				}
			}

			//过滤空值并重新从0开始排序
			$actioninfos = array_values(array_filter($actioninfos));
			//所拥有的动作
			foreach($actioninfos as $actioninfo){
				$actions[] =$actioninfo['action'];
			}
			$actions = array_unique($actions);
			//所拥有的组别id
			foreach($actioninfos as $actioninfo){
				$groupids[] = $actioninfo['groupid'];
			}

			$groupids = array_unique($groupids);

			//所拥有的组别信息
			$groupinfos = $CI->rolecontrol_m->get_groups_by_groupid($groupids);

			//所拥有的menuid
			foreach($groupinfos as $groupinfo){
				$menuids[] = $groupinfo['menuid'];
			}

			$menuids = array_unique($menuids);

			//所拥有的menu信息
			$menuinfos = $CI->rolecontrol_m->get_menus_by_menuid($menuids);

			//生成menulist
			for($i=0;$i<count($groupinfos);$i++){
				for($j=0;$j<count($actioninfos);$j++){
					if($actioninfos[$j]['groupid']==$groupinfos[$i]['groupid']){
						$groupinfos[$i]['actions'][]=$actioninfos[$j];
					}
				}
			}

			for($i=0;$i<count($menuinfos);$i++){
				for($j=0;$j<count($groupinfos);$j++){
					if($menuinfos[$i]['menuid']==$groupinfos[$j]['menuid']){
						$menuinfos[$i]['groups'][]=$groupinfos[$j];
					}
				}
			}
			return $menuinfos;

		}
 }
?>
